﻿# based on code from http://blog.octavie.nl/index.php/2012/01/27/deploying-solutions-with-language-packs-with-powershell/

param(     [string]$solution,     		      
		   [switch]$deploy = $true,     
		   [switch]$remove = $false,     
		   [switch]$update = $true)
 
Add-PSSnapIn "Microsoft.SharePoint.PowerShell" -ea silentlycontinue

 if ($deploy -or $update) {
	Write-Host ""
	Write-Host -ForegroundColor Yellow "Installing " -NoNewline
	Write-Host -ForegroundColor Green $solutionName -NoNewline
	Write-Host ""
   
	$solutionName=[IO.Path]::GetFileName($solution) 
	$sol = Get-SPSolution $solutionName -ErrorAction SilentlyContinue 
  
	if ($sol) 
	{ 
		if ($update) {
			Write-Host -ForegroundColor yellow "$solutionName already installed in this farm"
			Write-Host -ForegroundColor yellow "Upgrading.." -NoNewline
			Update-SPSolution -identity $sol -Literalpath $solution -GacDeployment -CasPolicies
		}
		else {
			Write-Host -ForegroundColor yellow "$solutionName already installed in this farm."
		}
	} 
	else 
	{ 
		Write-Host -ForegroundColor yellow "Adding to the farm" -NoNewline
	   
		$sol = Add-SPSolution $solution
	   
		Write-Host -ForegroundColor Green " Added"
			  
		Write-Host -ForegroundColor yellow "Deploying" -NoNewline
		Install-SPSolution -Identity $solutionName -GacDeployment -CasPolicies      
	} 
	
	$sol = Get-SPSolution $solutionName
  
	# Wait for solution to deploy
	if ($sol.Deployed -eq $false ) { 
		$counter = 1 
		while( ($sol.JobExists -eq $true ) -and ( $counter -lt 30  ) ) { 
			Write-Host -ForegroundColor yellow "." -NoNewline
			sleep 2
			$counter++ 
		} 
	} 
	
}
else {
	$solutionName=[IO.Path]::GetFileName($solution) 
	$sol = Get-SPSolution $solutionName -ErrorAction SilentlyContinue 

	if ($sol) 
	{ 
		 if ($sol.Deployed -eq $true ) { 
			Write-Host -ForegroundColor yellow "Uninstalling Solution." -nonewline
			UnInstall-SPSolution -Identity $solutionName  -confirm:$false

		$counter = 1 
		while( ($sol.JobExists -eq $true ) -and ( $counter -lt 40  ) ) { 
				Write-Host -ForegroundColor yellow "." -NoNewline
				sleep 2
				$counter++ 
			} 
		 }
		Write-Host "" 
		Write-Host -ForegroundColor yellow "Removing Solution." 
		sleep 2
		Remove-SPSolution -Identity $solutionName -confirm:$false
	}
	else
	{
		Write-Host -ForegroundColor yellow "Solution not installed." 
	}

	
}

Write-Host -ForegroundColor Green " Deployment Completed"
